home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: passing arrays and returning structs
- Date: 11 Mar 1996 21:54:57 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4i27fh$bbd@sparcserver.lrz-muenchen.de>
- References: <4i2128$69d@news2.acs.oakland.edu>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- jggoslin@vela.acs.oakland.edu (Monument) writes:
-
- >I got a mess of errors, included below for your perusal. If anyone
- >has any suggestions on how I could get the pointers and returns fixed,
- >I would sincerely appreciate it. Post or email, since I read both
- >regularly.
-
- >===client.h===
- >#include <stdio.h>
-
- [some irrelevant headers and definitions edited]
-
- >typedef struct my_msgbuf {
- > long mtype;
- > int pid;
- > int size;
- > int data[2*MAXSZ];
- >} Message;
- >===client.h===
-
- >===client.c===
- >// header files
- >#include "client.h"
-
- >// function prototypes
- >struct Message transitive_closure(int matrix[]);
-
- "struct Message" is an incomplete type. This seems to be the problem
- of the day. You have declared a type "struct my_msgbuf", and
- "Message" is an other name for it. "struct Message" is completely
- unrelated to both of them, but since it lives in a different
- namespace than "Message" - it is a struct "tag" - it may have the
- same name.
-
- >// MAINLINE
-
- This is a syntax error in C. In a similar language that allows this
- kind of comments, both "struct my_msgbuf" and "my_msgbuf" are
- known types, but "struct Message" is still an incomplete type.
-
- >int main(int argc, char *argv[])
- >{
-
- >[here is the relevant call]
- > // get the transitive closure
- > buffer = transitive_closure(matrix);
-
- [rest of main() edited]
-
- >struct Message* transitive_closure(int matrix[])
-
- Is there a good reason to provide a declaration with a different
- return type? Your compiler tells you what your problem is in
- _very_ clear words:
-
- >{
- > int i, j, size;
- > Message buffer;
- > pid_t pid;
-
- >[here do I refer to the subtypes in the correct manner? "." vs. "->"
- >is what I'm talking about]
-
- > buffer.mtype=1L;
-
- "buffer" is _not_ a pointer, so you can write either
-
- (&buffer)->mtype = 1L;
-
- or
-
- buffer.mytype = 1L
-
- Compilers tend to tell you if you use the wrong operator, and they are
- required to do so.
-
- > buffer.pid=pid;
- > buffer.size=size;
- > for (i=0; i<size; i++)
- > for (j=0; j<size; j++)
- > buffer.data[i*size+j]=matrix[i*size+j];
-
- >[is the return type correct?]
- > return *buffer;
-
- Obviously not. Your compiler say that you are trying to dereference
- a non-pointer. You want to return a pointer to a local variable.
- This cannot be done if the variable has storage class "auto". The
- syntax to return a pointer to a local variable with storage class
- "static" is "return &buffer;".
-
- How about sticking to your original declaration of your function
- and leaving the dirty work of figuring out how to pass a "struct
- my_msgbuf" to your compiler? in this case, "return buffer;" will
- do.
-
- >}
- >===client.c===
-
- Your compiler seems to be quite verbose in its diagnostic messages.
- Learn to trust it.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-